//
//  LineHeight.swift
//  WeiBoSwift
//
//  Created by Jacky Sun on 16/5/11.
//  Copyright © 2016年 syf. All rights reserved.
//

import UIKit


extension UILabel {
    
    /**
     适配给定lab 的行高
     
     - parameter spacing: spacing description
     - parameter maxSize: maxSize description
     - parameter text:    text description
     - returns:
     */
    func adaptLabLineSpacing(spacing: CGFloat, maxSize: CGSize, text: String)->CGSize {
        
        let attributeStr: NSMutableAttributedString = NSMutableAttributedString(string: text)
        
        let paragraphStyle: NSMutableParagraphStyle  = NSMutableParagraphStyle()
        
        paragraphStyle.lineSpacing = spacing
       
        attributeStr.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range:  NSMakeRange(0, text.characters.count))
        
        self.attributedText = attributeStr
        
        let size = self.sizeThatFits(maxSize)
        
        return size
    }
    
    
    /**
     传入一个attributedStr  然后再加一个段落的问题
     
     - parameter spacing:        <#spacing description#>
     - parameter maxSize:        <#maxSize description#>
     - parameter attributedText: <#attributedText description#>
     
     - returns: <#return value description#>
     */
    func adaptLabelByAttributeStr(spacing: CGFloat, maxSize: CGSize, attributedText: NSAttributedString)->CGSize {
        
        let attributeStr: NSMutableAttributedString = NSMutableAttributedString(attributedString: attributedText)
        
        let paragraphStyle: NSMutableParagraphStyle  = NSMutableParagraphStyle()
        
        paragraphStyle.lineSpacing = spacing
        
        attributeStr.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range:  NSMakeRange(0, text!.characters.count))
        
        self.attributedText = attributeStr
        
        let size = self.sizeThatFits(maxSize)
        
        return size
    }
    
}
